package com.lhf.fvsooa.service;

import com.lhf.common.util.Result;
import com.lhf.fvsooa.domain.User;
import com.lhf.userserviceapi.dto.FvsSysUserDTO;
import com.lhf.userserviceapi.service.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * <p></p>
 *
 * @author liuhf
 * @createTime 2021/4/1 9:56
 * @since 1.0
 */
@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {
//    @Resource(name="redisCacheManager")
//    private RedisCacheManager redisCacheManager;

    @DubboReference
    private UserService sysUserFeign;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        FvsSysUserDTO result = sysUserFeign.findUserByUsername(username);
        User user = null;
        FvsSysUserDTO dto = Optional.ofNullable(result).orElseThrow(() -> new UsernameNotFoundException("用户不存在"));
        user = new User(dto);
        return user;
    }

    public UserService getSysUserFeign() {
        return sysUserFeign;
    }

    public void setSysUserFeign(UserService sysUserFeign) {
        this.sysUserFeign = sysUserFeign;
    }
}
